﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Runtime.InteropServices;



namespace AllInOne.Utils
{
    public class PrinterUtil
    {

        /// <summary>
        /// 调用win api将指定名称的打印机设置为默认打印机
        /// </summary>
        /// <param name="PrinterDevice">打印机名称</param>
        /// <returns></returns>
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(String PrinterDevice); //





        public static PrinterStatus GetPrinterStat(string PrinterDevice)
        {

            try
            {
                PrinterStatus ret = 0;
                string path = @"win32_printer.DeviceId='" + PrinterDevice + "'";
                ManagementObject printer = new ManagementObject(path);
                printer.Get();

                //foreach (var p in printer.Properties)
                //{
                //    Console.WriteLine(p.Name + ":" + p.Value);

                //}

                Console.WriteLine(printer.Properties["PrinterStatus"].Value);
                ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
                return ret;
            }
            catch {
                return PrinterStatus.FREE;
            }
        }
    }

    public enum PrinterStatus {
        OTHER = 1,
        UNKNOWN,
        FREE,
        PRINT,
        WARM,
        STOP,
        PRINTING,
        OFFLINE
    }


}
